package io.bdmc.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期处理
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年12月21日 下午12:53:33
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {
    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String Pay_DATE_TIME_PATTERN = "yyyyMMddHHmmss";

    private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM" };

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static String formatPayDate(Date date) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(Pay_DATE_TIME_PATTERN);
            return df.format(date);
        }
        return null;
    }

    /**
     * 获取当前时间 yyyy-MM-dd HH:mm:ss
     */
    public static String getNowLocalDateTime() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
        return LocalDateTime.now().format(formatter);
    }

    /**
     * 获取当前时间 pattern
     */
    public static String getNowLocalDateTime(String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.now().format(formatter);
    }

    /**
     * 格式化LocalDateTime 为 指定pattern格式
     */
    public static String formatLocalDateTime(LocalDateTime date, String pattern) {
        if (date != null) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
            return date.format(df);
        }
        return null;
    }

    public static String formatLocalDateTimeDefault(LocalDateTime date) {
        if (date != null) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
            return date.format(df);
        }
        return null;
    }

    public static LocalDateTime stringToLocalDateTime(String date) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(date, df);
        return ldt;
    }

    public static LocalDate stringToLocalDate(String date) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate ldt = LocalDate.parse(date, df);
        return ldt;
    }

    /**
     * 将时间转换为多少天、小时、分钟
     * 
     * @param cost 总共多少分钟
     */
    public static String getDayHourMinute(int cost) {
        int day = cost / (60 * 24);
        int hour = (cost - day * (60 * 24)) / (60);
        int min = cost - day * (60 * 24) - hour * (60);
        String dayStr = "天";
        String hourStr = "小时";
        String minStr = "分钟";
        String time = "";
        if (day > 0) {
            time = day + dayStr + hour + hourStr + min + minStr;
        } else {
            if (hour > 0) {
                time = hour + hourStr + min + minStr;
            } else {
                time = min + minStr;
            }
        }
        return time;
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 将LocalDate转为yyyy-MM-dd
     */
    public static String formatLocalDateDefault(LocalDate date) {
        if (date != null) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_PATTERN);
            return date.format(df);
        }
        return null;
    }

    /**
     * 将LocalDate转为 pattern 格式
     */
    public static String formatLocalDateDefault(LocalDate date, String pattern) {
        if (date != null) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
            return date.format(df);
        }
        return null;
    }



    public static String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }
}
